﻿// Serotonin -- easy-to-use image filters application
// https://code.google.com/p/serotonin/
//
// Copyright © Frank Nagl, 2012-2013
// admin@franknagl.de
//
namespace Serotonin.OptionsForms
{
    using Filter;
    using Serotonin.Controls;
    using System;
    using System.Drawing;
    using System.Windows.Forms;        

    /// <summary>Filter specified properties form.</summary>
    public partial class ConvolutionForm : Form, IOptionsForm
    {
        private readonly Convolution5X5 filter;

        /// <summary>
        /// The processor for managing the images and the image GUI panel.
        /// </summary>
        public Processor Processor { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ConvolutionForm"/> class.
        /// </summary>
        /// <param name="processor">The processor.</param>
        public ConvolutionForm(Processor processor)
        {
            InitializeComponent();
            filter = AOF.Init(processor, this, gbProcess,
                                new Convolution5X5()) as Convolution5X5;
        }

                /// <summary> Applies the specified filter in the options form. </summary>
        public void ApplyFilter()
        {
            // grayscale example
            Bitmap b = filter.Apply(Processor.OriginalImage);
                    string s = Program.L.Text[40] + Program.L.Text[83] + ": "; // +
                        //Program.L.Text[84] + ": R=" + filter.Red + "; " +
                        //"G=" + filter.Green + "; " +
                        //"B=" + filter.Blue + "; ";
            Processor.Change(b, s); 
        }

        /// <summary>
        /// Sets the language specified string texts for the form elements.
        /// </summary>
        public void SetStrings()
        {
            // Process groupbox
            gbProcess.Text = Program.L.Text[54];
            btnProcess.Text = Program.L.Text[55];
            btnReset.Text = Program.L.Text[56];

            Options.Text = Program.L.Text[125]; // Name of filter
            tabctrlOptions.TabPages[0].Text = Program.L.Text[57]; // Options

            gbPredefinedFilter.Text = Program.L.Text[132];
            rdBtnLaplace.Text = Program.L.Text[127];
            rdBtnLaplace2.Text = Program.L.Text[128];
            rdBtnGaussianBlur.Text = Program.L.Text[129];
            rdBtnSimpleBlur.Text = Program.L.Text[130];
            gbKernel.Text = Program.L.Text[131];
        }

        private void BtnProcessClick(object sender, EventArgs e)
        {
            EndlessProgressBarFormInThread form =
                new EndlessProgressBarFormInThread(
                    Program.L.Text[63],
                    Program.L.Text[64]);
            form.Start();

            try
            {
                ApplyFilter();
            }
            catch (ArgumentException aex)
            {
                MessageBox.Show(aex.Message);
                BtnResetClick(sender, e);
            }

            form.End();
        }

        private void BtnResetClick(object sender, EventArgs e)
        {
            Processor.Reset();
        }

        #region kernel numeric up down boxes
        private void NumericUpDown1ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(0, (int)numericUpDown1.Value);
        }

        private void NumericUpDown2ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(1, (int)numericUpDown2.Value);
        }

        private void NumericUpDown3ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(2, (int)numericUpDown3.Value);
        }

        private void NumericUpDown4ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(3, (int)numericUpDown4.Value);
        }

        private void NumericUpDown5ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(4, (int)numericUpDown5.Value);
        }

        private void NumericUpDown6ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(5, (int)numericUpDown6.Value);
        }

        private void NumericUpDown7ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(6, (int)numericUpDown7.Value);
        }

        private void NumericUpDown8ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(7, (int)numericUpDown8.Value);
        }

        private void NumericUpDown9ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(8, (int)numericUpDown9.Value);
        }

        private void NumericUpDown10ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(9, (int)numericUpDown10.Value);
        }

        private void NumericUpDown11ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(10, (int)numericUpDown11.Value);
        }

        private void NumericUpDown12ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(11, (int)numericUpDown12.Value);
        }

        private void NumericUpDown13ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(12, (int)numericUpDown13.Value);
        }

        private void NumericUpDown14ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(13, (int)numericUpDown14.Value);
        }

        private void NumericUpDown15ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(14, (int)numericUpDown15.Value);
        }

        private void NumericUpDown16ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(15, (int)numericUpDown16.Value);
        }

        private void NumericUpDown17ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(16, (int)numericUpDown17.Value);
        }

        private void NumericUpDown18ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(17, (int)numericUpDown18.Value);
        }

        private void NumericUpDown19ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(18, (int)numericUpDown19.Value);
        }

        private void NumericUpDown20ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(19, (int)numericUpDown20.Value);
        }

        private void NumericUpDown21ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(20, (int)numericUpDown21.Value);
        }

        private void NumericUpDown22ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(21, (int)numericUpDown22.Value);
        }

        private void NumericUpDown23ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(22, (int)numericUpDown23.Value);
        }

        private void NumericUpDown24ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(23, (int)numericUpDown24.Value);
        }

        private void NumericUpDown25ValueChanged(object sender, EventArgs e)
        {
            filter.SetKernelCoefficient(24, (int)numericUpDown25.Value);
        }
        #endregion

        #region Pre-defined filter (radio button events)

        private void RdBtnGaussianBlurCheckedChanged(object sender, EventArgs e)
        {
            if (rdBtnGaussianBlur.Checked)
            {
                filter.Kernel = Convolution5X5.GaussianBlurKernel;
                SetnumericUpDownBoxes(Convolution5X5.GaussianBlurKernel);                
            }
        }

        private void RdBtnLaplaceCheckedChanged(object sender, EventArgs e)
        {
            if (rdBtnLaplace.Checked)
            {
                filter.Kernel = Convolution5X5.LaplaceKernel;
                SetnumericUpDownBoxes(Convolution5X5.LaplaceKernel);
            }
        }

        private void RdBtnLaplace2CheckedChanged(object sender, EventArgs e)
        {
            if (rdBtnLaplace2.Checked)
            {
                filter.Kernel = Convolution5X5.LaplaceKernel2;
                SetnumericUpDownBoxes(Convolution5X5.LaplaceKernel2);
            }
        }

        private void RdBtnSimpleBlurCheckedChanged(object sender, EventArgs e)
        {
            if (rdBtnSimpleBlur.Checked)
            {
                filter.Kernel = Convolution5X5.LinearBlur;
                SetnumericUpDownBoxes(Convolution5X5.LinearBlur);                
            }
        }
        #endregion Pre-defined filter (radio button events)

        private void SetnumericUpDownBoxes(int[] newKernel)
        {
            numericUpDown25.Value = newKernel[24];
            numericUpDown24.Value = newKernel[23];
            numericUpDown23.Value = newKernel[22];
            numericUpDown22.Value = newKernel[21];
            numericUpDown21.Value = newKernel[20];
            numericUpDown20.Value = newKernel[19];
            numericUpDown19.Value = newKernel[18];
            numericUpDown18.Value = newKernel[17];
            numericUpDown17.Value = newKernel[16];
            numericUpDown16.Value = newKernel[15];
            numericUpDown15.Value = newKernel[14];
            numericUpDown14.Value = newKernel[13];
            numericUpDown13.Value = newKernel[12];
            numericUpDown12.Value = newKernel[11];
            numericUpDown11.Value = newKernel[10];
            numericUpDown10.Value = newKernel[9];
            numericUpDown9.Value = newKernel[8];
            numericUpDown8.Value = newKernel[7];
            numericUpDown7.Value = newKernel[6];
            numericUpDown6.Value = newKernel[5];
            numericUpDown5.Value = newKernel[4];
            numericUpDown4.Value = newKernel[3];
            numericUpDown3.Value = newKernel[2];
            numericUpDown2.Value = newKernel[1];
            numericUpDown1.Value = newKernel[0];
        }
    }
}
